let express = require('express')
let token = require('jsonwebtoken')
let pool = require('../mysql/index')
let config = require('./config')


exports.login = (req, res) => {
  if (!req.body.username || !req.body.password) {
    return res.send("用户名和密码不能为空")
  }
  pool.query('select * from user;', (err, result) => {
    if (err) return console.log(err.message)
    let tokenstr = ""
    result.forEach(item => {
      // 密码不能放进token字符串里面
      const user = { ...item, password: '' }
      tokenstr += "bearer" + token.sign(user, config.secret, { expiresIn: config.expiresin })
      // console.log(tokenstr);
    })
    res.send({
      state: "1",
      message: "登录成功",
      token: tokenstr
    })

  })
}
exports.register = (req, res) => {
  let sqlstr = 'insert into user value(?,?,?.?)'
  pool.query(sqlstr, [req.body.uid, req.body.uname, req.body.uphonenumber, req.body.uaddress], (err, result) => {
    if (err) return console.log(err.message)
    console.log(result);
    res.send("登录成功")
  })

}
